#ifndef DYNAMIC_LIBRARY_H
#define DYNAMIC_LIBRARY_H

#include <windows.h>
#include <string>
#include <iostream>

class DynamicLibrary
{
public:
    DynamicLibrary(const std::string &libraryName);
    ~DynamicLibrary();

    template <typename T>
    T GetFunction(const std::string &functionName)
    {
        if (!hModule)
        {
            std::cerr << "Library not loaded. Cannot get function address." << std::endl;
            return nullptr;
        }

        T func = reinterpret_cast<T>(GetProcAddress(hModule, functionName.c_str()));
        if (!func)
        {
            std::cerr << "Failed to get address for function: " << functionName << std::endl;
        }
        return func;
    }

private:
    std::string libraryName; // DLL名称
    HMODULE hModule;         // DLL模块句柄

    // 禁止拷贝构造和赋值操作
    DynamicLibrary(const DynamicLibrary &) = delete;
    DynamicLibrary &operator=(const DynamicLibrary &) = delete;
};

#endif // DYNAMIC_LIBRARY_H